//
//  JSSelectGenderView.m
//  NiubilityProject
//
//  Created by Apple on 2017/11/20.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "JSSelectGenderView.h"

@interface JSSelectGenderView ()<UIPickerViewDelegate,UIPickerViewDataSource>


@end

@implementation JSSelectGenderView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        JS_WeakSelf(self)
        // 1.顶部视图
        UIView *topView = [UIView new];
        topView.backgroundColor = JS_colorWithHex(kBodyColor);
        [self addSubview:topView];
        [topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.and.left.and.right.equalTo(weakself);
            make.height.mas_equalTo(40);
        }];
        // 1.1 关闭按钮
        UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [closeBtn addTarget:self action:@selector(hidenMaskView) forControlEvents:UIControlEventTouchUpInside];
        [closeBtn setImage:[UIImage qmui_imageWithShape:QMUIImageShapeNavClose size:CGSizeMake(15, 15) tintColor:[UIColor whiteColor]] forState:UIControlStateNormal];
        [topView addSubview:closeBtn];
        closeBtn.frame = CGRectMake(0, 0, 40, 40);
        //        [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        //            make.top.and.left.and.bottom.equalTo(topView);
        //            make.width.mas_equalTo(kRealValue(80));
        //        }];
        // 1.2 完成
        UIButton *finishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [finishBtn addTarget:self action:@selector(selectFinish:) forControlEvents:UIControlEventTouchUpInside];
        finishBtn.titleLabel.font = UIFontMake(15);
        [finishBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [finishBtn setTitle:JSLocalizedString(kLocalizableFinish) forState:UIControlStateNormal];
        [topView addSubview:finishBtn];
        [finishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.and.right.and.bottom.equalTo(topView);
            make.width.mas_equalTo(kRealValue(80));
        }];
        // 1.3 描述
        UILabel *desL = [UILabel new];
        desL.textColor = [UIColor whiteColor];
        desL.font = UIFontMake(14);
        desL.textAlignment = NSTextAlignmentCenter;
        desL.text = JSLocalizedString(@"genderLoca");
        [topView addSubview:desL];
        [desL sizeToFit];
        [desL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(topView);
        }];
        // 2 picker view
        _pickerView = [UIPickerView new];
        [self addSubview:_pickerView];
        _pickerView.dataSource = self;
        _pickerView.delegate   = self;
        [_pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.and.bottom.equalTo(weakself);
            make.top.equalTo(topView.mas_bottom);
        }];
        self.selectGenderStr = JSLocalizedString(@"genderMan");

    }
    return self;
}

// 返回多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
    
}
// 返回多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return 2;
    
}
// 返回每行的标题
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (row) {
        return JSLocalizedString(@"genderWoman");
    }
    return JSLocalizedString(@"genderMan");
    
}
// 选中行显示在label上
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (row) {
        self.selectGenderStr = JSLocalizedString(@"genderWoman");
    }else{
        self.selectGenderStr = JSLocalizedString(@"genderMan");
    }
}

-(void)selectFinish:(UIButton *)sender {
    UserGender gender = UserGenderUnKnow;
    if ([self.selectGenderStr isEqualToString:JSLocalizedString(@"genderWoman")]) {
        gender = UserGenderFemale;
    }else if ([self.selectGenderStr isEqualToString:JSLocalizedString(@"genderMan")]){
        gender = UserGenderMale;
    }
    if ([self.delegate respondsToSelector:@selector(didFinishGender:Gender:)]) {
        [self.delegate didFinishGender:self.selectGenderStr Gender:gender];
    }
    [self hidenMaskView];
}
@end
